package org.example.config;

import org.springframework.beans.factory.annotation.Value;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @description: redisson配置,用于分布式锁
 * @author: 谢超
 * @date: 2023/07/23 11:33
 **/
@Configuration
public class RedissionConfiguration {

    @Value("${spring.redis.host}")
    private String redisHost;

    @Value("${spring.redis.port}")
    private String redisPort;

    @Value("${spring.redis.password}")
    private String redisPwd;

    /**
     * 配置分布式锁的redisson
     * @return
     */
    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();

        // 单机模式
        config.useSingleServer().setPassword(redisPwd).setAddress("redis://" + redisHost + ":" + redisPort);

        // 集群模式
        // config.useClusterServers().addNodeAddress("redis://127.0.0.1:7000", "redis://127.0.0.1:7001");

        RedissonClient redissonClient = Redisson.create(config);

        return redissonClient;
    }

    /**
     * 集群模式
     * 备注可以用”rediss://"来启用SSL连接
     * @return
     */
//    @Bean
//    public RedissonClient redissonClusterClient() {
//        Config config = new Config();
//
//        // 集群模式
//        config.useClusterServers()
//                .setScanInterval(2000)
//                .addNodeAddress("redis://127.0.0.1:7000")
//                .addNodeAddress("redis://127.0.0.1:7001");
//        RedissonClient redissonClient = Redisson.create(config);
//        return redissonClient;
//    }

}
